/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package roomarrange;

import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Calendar;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.TextArea;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import javax.faces.FacesException;
import javax.faces.convert.IntegerConverter;
import javax.sql.DataSource;
import com.sun.webui.jsf.model.Option;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version approve.java
 * @version Created on 2009-9-28, 23:56:44
 * @author ben
 */
public class approve extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        t_roomsetDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.t_roomsetRowSet}"));
    }
    private SingleSelectOptionsList dropDown1DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getDropDown1DefaultOptions() {
        return dropDown1DefaultOptions;
    }

    public void setDropDown1DefaultOptions(SingleSelectOptionsList ssol) {
        this.dropDown1DefaultOptions = ssol;
    }
    private SingleSelectOptionsList dropDown2DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getDropDown2DefaultOptions() {
        return dropDown2DefaultOptions;
    }

    public void setDropDown2DefaultOptions(SingleSelectOptionsList ssol) {
        this.dropDown2DefaultOptions = ssol;
    }
    private Calendar calendar1 = new Calendar();

    public Calendar getCalendar1() {
        return calendar1;
    }

    public void setCalendar1(Calendar c) {
        this.calendar1 = c;
    }
    private DropDown dropDown1 = new DropDown();

    public DropDown getDropDown1() {
        return dropDown1;
    }

    public void setDropDown1(DropDown dd) {
        this.dropDown1 = dd;
    }
    private Calendar calendar2 = new Calendar();

    public Calendar getCalendar2() {
        return calendar2;
    }

    public void setCalendar2(Calendar c) {
        this.calendar2 = c;
    }
    private DropDown dropDown2 = new DropDown();

    public DropDown getDropDown2() {
        return dropDown2;
    }

    public void setDropDown2(DropDown dd) {
        this.dropDown2 = dd;
    }
    private TextField textField1 = new TextField();

    public TextField getTextField1() {
        return textField1;
    }

    public void setTextField1(TextField tf) {
        this.textField1 = tf;
    }
    private TextField textField2 = new TextField();

    public TextField getTextField2() {
        return textField2;
    }

    public void setTextField2(TextField tf) {
        this.textField2 = tf;
    }
    private TextField textField3 = new TextField();

    public TextField getTextField3() {
        return textField3;
    }

    public void setTextField3(TextField tf) {
        this.textField3 = tf;
    }
    private TextField textField4 = new TextField();

    public TextField getTextField4() {
        return textField4;
    }

    public void setTextField4(TextField tf) {
        this.textField4 = tf;
    }
    private TextArea textArea1 = new TextArea();

    public TextArea getTextArea1() {
        return textArea1;
    }

    public void setTextArea1(TextArea ta) {
        this.textArea1 = ta;
    }

    // </editor-fold>
    meetings mt = new meetings();
    //生成临时数据
    ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig.xml");
    DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
    //DataSource ds = (DataSource) ctx.getBean("dataSourcePool");
    DataJdbcDao taskDao = new DataJdbcDao();
    private CachedRowSetDataProvider t_roomsetDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getT_roomsetDataProvider() {
        return t_roomsetDataProvider;
    }

    public void setT_roomsetDataProvider(CachedRowSetDataProvider crsdp) {
        this.t_roomsetDataProvider = crsdp;
    }
    private IntegerConverter dropDown3Converter = new IntegerConverter();

    public IntegerConverter getDropDown3Converter() {
        return dropDown3Converter;
    }

    public void setDropDown3Converter(IntegerConverter ic) {
        this.dropDown3Converter = ic;
    }
    private DropDown dropDown3 = new DropDown();

    public DropDown getDropDown3() {
        return dropDown3;
    }

    public void setDropDown3(DropDown dd) {
        this.dropDown3 = dd;
    }

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public approve() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("approve Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
        String app = this.getSessionBean1().getSApp();
        Option[] opt = mainHelper.getTimeQuantum(app);
        this.getDropDown1DefaultOptions().setOptions(opt);
        this.getDropDown2DefaultOptions().setOptions(opt);

    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
        String sMeetingid;
        String sTime;
        String eTime;
        Date sDate;
        Date eDate;
        String sCname;
        String sDept;
        String sStatus;
        String sAbstract;
        int iRoomid;
        sMeetingid = this.getSessionBean1().getSUUID();
        taskDao.setDataSource(ds);
        String sapp = this.getSessionBean1().getSApp();
        mt = taskDao.getMeeting(sMeetingid,sapp);
        sTime = mt.getSStime();
        eTime = mt.getSEtime();
        sDate = mt.getDSdate();
        eDate = mt.getDEdate();
        sCname = mt.getSCname();
        sDept = mt.getSCinitdept();
        sStatus = mt.getCStatus();
        sAbstract = mt.getSCabstract();
        iRoomid = mt.getIRoomid();
        this.getDropDown1().setSelected(sTime);
        this.getDropDown2().setSelected(eTime);
        this.getCalendar1().setText(sDate);
        this.getCalendar2().setText(eDate);
        this.getTextField1().setText(sCname);
        this.getTextField2().setText(sDept);
        this.getTextField4().setText(sStatus);
        this.getTextArea1().setText(sAbstract);
        this.getDropDown3().setSelected(iRoomid);
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
        t_roomsetDataProvider.close();
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public String button1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        //同意召开
        String sOpinion = "";
        if(this.getTextField3().getText()==null){
            sOpinion="同意";
        }else{
            sOpinion = this.getTextField3().getText().toString();
        }
        int iGrp;
        int ibj = 0;
        if (this.getDropDown3().getSelected() == null) {
            info("请选择会议室");
            return null;
        }
        String ls_sdate = "";
        String ls_edate = "";
        String ls_stime = "";
        String ls_etime = "";
        String ls_room = "";
        ls_stime = this.getDropDown1().getSelected().toString();
        ls_etime = this.getDropDown2().getSelected().toString();
        this.getSessionBean1().setSSdate(ls_stime);
        this.getSessionBean1().setSEdate(ls_etime);
        int li_usrid = this.getSessionBean1().getIusrid();
        ls_room = this.getDropDown3().getValue().toString();
        Date ld_sdate = null;
        Date ld_edate = null;
        Date ldt_sdate = null;
        Date ldt_edate = null;
        Date ldt_now = null;
        long ll_s = 0;
        long ll_e = 0;
        long ll_now = 0;
        int li_cnt = 0;
        ld_sdate = (Date) this.getCalendar1().getValue();
        ld_edate = (Date) this.getCalendar2().getValue();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ls_sdate = sdf.format(ld_sdate);
        ls_edate = sdf.format(ld_edate);
        ls_sdate = ls_sdate + " " + ls_stime + ":00";
        ls_edate = ls_edate + " " + ls_etime + ":00";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            ldt_sdate = sdf2.parse(ls_sdate);
            ldt_edate = sdf2.parse(ls_edate);
        } catch (ParseException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
        ldt_now = new Date();
        ll_s = ldt_sdate.getTime();
        ll_e = ldt_edate.getTime();
        ll_now = ldt_now.getTime();
        if (ll_s >= ll_e) {
            info("开始时间必须早于截止时间");
            return null;

        }
        /*
        if (ll_s < ll_now) {
            info("开始时间必须晚于当前时间");
            return null;

        }
         */
        Integer li_roomid = (Integer) this.getDropDown3().getSelected();
        String sMeetingid = this.getSessionBean1().getSUUID();
        taskDao.setDataSource(ds);
        //取会议室类别
        int li_roomsort = taskDao.getRoomSort(li_roomid);
        int li_confirmgrpid = taskDao.getRoomConfirmGrpId(li_roomid);
        //先到先得的情况需要判断下会议室的占用情况
        String sapp = this.getSessionBean1().getSApp();
        li_cnt = taskDao.getMeetingCnt(ls_sdate, ls_edate, li_roomid,sapp);
        if (li_cnt > 0) {
            info("提示，该时间段会议室已有人占用，请重新选择！");
            return null;
        }
        //更新一部分内容，包括时间和地点
        taskDao.updateTimeNroom(sMeetingid, ls_sdate, ls_edate, ls_stime, ls_etime, li_roomid,sapp);
        //取得更新过的
        mt = taskDao.getMeeting(sMeetingid,sapp);
        iGrp = mt.getIConfirmByGrpId();

        int iStatus;
        int iUsrid;
        int iroomid;
        iUsrid = this.getSessionBean1().getIusrid();
        iroomid = (Integer) this.getDropDown3().getSelected();
        String sTip = null;

        //办公室确认
        iStatus = 5;
        sTip = "会议已成功核准！";
        //最后确认
        //确认一个会议,bj=0 为办公室确认 bj=1为科室确认
        ibj = 0;
        taskDao.confirmMeeting(sMeetingid, sOpinion, iStatus, iUsrid, ibj);
        //重新取得mt
        mt = taskDao.getMeeting(sMeetingid,sapp);
        //给所有参会人员发消息
        ibj = 1;
        setupParam stp = new setupParam();
        stp = taskDao.getParam();
        taskDao.sendRTX(mt, ibj,stp);

        //显示提示信息
        this.getSessionBean1().setSTip(sTip);
        //cls
        this.getSessionBean1().setIMtHost(0);
        this.getSessionBean1().setIMtInitdept(0);
        this.getSessionBean1().setIroomid(0);
        this.getSessionBean1().setSEdate("");
        this.getSessionBean1().setSSdate("");
        this.getSessionBean1().setSUUID("");
        this.getSessionBean1().setSMtAbstract("");
        this.getSessionBean1().setSMtName("");
        this.getSessionBean1().setSMtNotice("");
        this.getSessionBean1().setSRefMtId("");

        return "toSucc";
    }

    public String button2_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        //否决一个会议
        String sOpinion = "";
        if(this.getTextField3().getText()==null){
            sOpinion="不同意";
        }else{
             sOpinion = this.getTextField3().getText().toString();
        }
   
        int iGrp;
        int ibj = 0;

        String sMeetingid = this.getSessionBean1().getSUUID();
        taskDao.setDataSource(ds);
        String sapp = this.getSessionBean1().getSApp();
        mt = taskDao.getMeeting(sMeetingid,sapp);
        iGrp = mt.getIConfirmByGrpId();

        int iStatus;
        int iUsrid;
        iUsrid = this.getSessionBean1().getIusrid();
        iStatus = 4;
        String sTip = "会议已成功否决，相关提示信息已发往申请人！";
        //最后确认
        taskDao.vetoMeeting(sMeetingid, sOpinion, iStatus, iUsrid, ibj,sapp);
        //重新取得mt
        mt = taskDao.getMeeting(sMeetingid,sapp);
        //给申请人发信息
        //给办公室发信息
        ibj = 4;
        setupParam stp = new setupParam();
        stp = taskDao.getParam();
        taskDao.sendRTX(mt, ibj,stp);
        //显示提示信息
        this.getSessionBean1().setSTip(sTip);
        //cls
        this.getSessionBean1().setIMtHost(0);
        this.getSessionBean1().setIMtInitdept(0);
        this.getSessionBean1().setIroomid(0);
        this.getSessionBean1().setSEdate("");
        this.getSessionBean1().setSSdate("");
        this.getSessionBean1().setSUUID("");
        this.getSessionBean1().setSMtAbstract("");
        this.getSessionBean1().setSMtName("");
        this.getSessionBean1().setSMtNotice("");

        return "toSucc";
    }
}

